package autoteam.ui.dialogs;

import java.awt.*;
import javax.swing.*;

import db.*;
import autoteam.tables.AutoTeamTableModel;

public class SearchCoachDialog extends AutoTeamDialog
{
	private static final long serialVersionUID = 1L;
	
	private DataAccessObject dao;
	
	private JLabel coach;
	private final JLabel teamLabel = new JLabel("Team: ");
	private final JLabel bioLabel = new JLabel("Biography:");
	
	private JLabel team;
	private JLabel bio;
	
	public SearchCoachDialog(JFrame dialogOwner, AutoTeamTableModel model, int row)
	{
		super (dialogOwner);
		
		String temp = (String) model.getValueAt(row, 0);
		coach = new JLabel(temp);
		
		String[] token = temp.split(", ");
		String lastname = token[0];
		String firstname = token[1];
		
		dao = new DataAccessObject();
		ColumnData col0 = new ColumnData("lastname", lastname);
		ColumnData col1 = new ColumnData("firstname", firstname);
		DBRecord record = dao.getRecord("coaches", col0, col1);
		
		int teamId = (Integer) record.getFieldValue("teamId");
		ColumnData idCol = new ColumnData("id", teamId);
		DBRecord rec = dao.getRecord("teams", idCol);
		
		if (rec.getFieldValue("teamname") != null)
			team = new JLabel(rec.getFieldValue("teamname").toString());
		else
			team = new JLabel("not found");
		bio = new JLabel(record.getFieldValue("biography").toString());
		
		// set dialog preferences
		int dialogWidth = 400;
		int dialogHeight = 400;
		this.setDialogSize(new Dimension(dialogWidth, dialogHeight));
		Toolkit sizer = Toolkit.getDefaultToolkit();
		Dimension screenSize = sizer.getScreenSize();
		int screenCenterX = (screenSize.width - dialogWidth) / 2;
		int screenCenterY = (screenSize.height - dialogHeight) / 2;
		this.setLocation(screenCenterX, screenCenterY);
		
		// set layout preferences
		setLayout(null);
		int x = 50;		// x coordinate
		int y = 50;		// y coordinate
		int xGap = 100;	// gap between columns
		int yGap = 30;	// gap between rows
		
		int x1 = 100;	// column one width
		int x2 = 200;	// column two width
		int y1 = 20;	// row height
		
		int i = 0;
		coach.setBounds(x, y, x1 + x2, y1);
		coach.setHorizontalAlignment(JLabel.CENTER);
		i++;
		i++;
		teamLabel.setBounds(x, y + (yGap * i), x1, y1);
		team.setBounds(x + xGap, y + (yGap * i++), x2, y1);
		bioLabel.setBounds(x, y + (yGap * i), x1, y1);
		bio.setBounds(x + xGap, y + (yGap * i++), x2, 200);
		bio.setVerticalAlignment(JLabel.TOP);
		
		// add components
		add(coach);
		add(teamLabel);
		add(team);
		add(bioLabel);
		add(bio);
	}

	@Override
	protected void setButtonActions(AutoTeamDialog thisDialog)
	{
		// TODO Auto-generated method stub
		
	}
}
